package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.missilike.core.domain.alipay.AlipayCashWithdrawalOrder;
import com.missilike.core.domain.alipay.AlipayCashWithdrawalResult;
import com.missilike.core.domain.alipay.AlipayNotifyResult;
import com.missilike.core.domain.alipay.AlipayOrder;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.IAlipayService;
import com.missilike.core.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2018-12-14 0:25
 **/
@Slf4j
@RestController
@RequestMapping("/api/pay")
public class AlipayController {

    private IAlipayService alipayService;

    public AlipayController(IAlipayService alipayService) {
        this.alipayService = alipayService;
    }

    @ApiOperation(value = "alipay", notes = "发起支付")
    @RequestMapping(value = "/alipay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult alipay(@RequestBody AlipayOrder order){
        log.info("Start to execute /pay/alipay body request={}", order);
        String result = alipayService.alipay(order);
        log.info("Start to execute /pay/alipay responese={}", JSON.toJSON(result));
        return ResultUtils.ok(result);
    }

    @ApiOperation(value = "notify", notes = "支付宝支付成功后.回调该接口")
    @RequestMapping(value = "/notify", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult notify(HttpServletRequest request, HttpServletResponse response){
        log.info("Start to execute /pay/alipay body request={}", request);
        AlipayNotifyResult result = alipayService.signVerified(request);
        log.info("Start to execute /pay/alipay responese={}", JSON.toJSON(result));
        return ResultUtils.ok(result);
    }

    @ApiOperation(value = "alipayCashWithdrawal", notes = "支付提现")
    @RequestMapping(value = "/alipayCashWithdrawal", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult alipayCashWithdrawal(@RequestBody AlipayCashWithdrawalOrder order) throws AlipayApiException {
        log.info("Start to execute /pay/alipayCashWithdrawal body request={}", order);
        AlipayCashWithdrawalResult result = alipayService.alipayCashWithdrawal(order);
        log.info("Start to execute /pay/alipayCashWithdrawal responese={}", JSON.toJSON(result));
        return ResultUtils.ok(result);
    }
}
